﻿@{
    Layout = null;// "~/Views/Shared/_Layout.cshtml";
}
@Html.Hidden("PermissionIds")
@Html.Hidden("ResourceId")
@Html.AntiForgeryToken()
<table id="PermissionTable" class="table table-striped table-hover dt-responsive display nowrap" cellspacing="0" width="100%"></table>
<script>
    var Permissions = {
        Search: function () {
            $("#PermissionTable").mainToolDataTable({
                "ajax": {
                    url: '@Url.Action("PagePermission","Permissions")',
                    type: 'post',
                    data: {
                        filter: {
                            Name: "",
                            Code: "",
                        }
                    }
                },
                "columns": [
                    { data: "Name", title: "权限名称" },
                    { data: "Code", title: "权限编号" },
                    {
                        title: "全选<input id='checkAll' type='checkbox' />",
                        orderable: false,
                        data: "Id",
                        style: "min-width:190px;",
                        render: function (data, type, row) {
                            return "<input class='permissiionIdClass' type='checkbox' value='" + data + "' " + ($.inArray(data, $.parseJSON($("#PermissionIds").val())) >= 0 ? "checked='checked'" : "") + " />";
                        }
                    },
                ],
                "initComplete": function () {
                    $(".permissiionIdClass").click(function () {
                        $("#checkAll").prop("checked", $("input.permissiionIdClass").length == $("input.permissiionIdClass:checked").length ? true : false);
                    });
                    $("#checkAll").prop("checked", $("input.permissiionIdClass").length == $("input.permissiionIdClass:checked").length ? true : false);
                }
            });
        },
    };
    //$(".permissiionIdClass").each(function (e, item) { item.checked = $.inArray(Number($(item).val()), $.parseJSON($("#PermissionIds").val())) >= 0 });
    Resources.SubmitEditPermission = function () {
        var selectPermissiionIds = new Array();
        $(".permissiionIdClass").each(function (e, item) {
            if (item.checked) {
                selectPermissiionIds.push(Number($(item).val()));
            }
        });
        $.mainTool.ajax({
            url: '@Url.Action("EditPermissionSubmit")',
            data: {
                ResourceId: Number($("#ResourceId").val()),
                PermissiionIds: $.parseJSON($("#PermissionIds").val()),
                SelectPermissiionIds: selectPermissiionIds,
                '__RequestVerificationToken': $("input[name='__RequestVerificationToken']").val()
            },
            success: function (data) {
                Resources.Search();
            }
        });
    };
    $(document).ready(function () {
        Permissions.Search();
        $("#checkAll").click(function () {
            $('input.permissiionIdClass').prop("checked", $(this).prop("checked"));
        });
    });
</script>